import cv2 as cv
# Load two images
img1 = cv.imread('dog.jpg')
img2 = cv.imread('pic1.png')
# I want to put logo on moon, So I create a ROI
offset=130
rows,cols,channels = img2.shape
roi = img1[offset:rows+offset, offset:cols+offset]
# Now create a mask of logo and create its inverse mask also
img2gray = cv.cvtColor(img2,cv.COLOR_BGR2GRAY)
ret, mask = cv.threshold(img2gray, 10, 255, cv.THRESH_BINARY)
mask_inv = cv.bitwise_not(mask)
# Now black-out the area of logo in ROI
# 取roi中与mask中不为零的值对应的像素，其他值为0，即在roi中扣出黑色的logo区域背景
img1_bg = cv.bitwise_and(roi,roi,mask = mask_inv)
# Take only region of logo from logo image.
# 取roi中与mask_inv中不为零的值对应的像素的值，其他值为0，即在logo图中扣出logo前景
img2_fg = cv.bitwise_and(img2,img2,mask = mask)
# Put logo in ROI and modify the main image
dst = cv.add(img1_bg,img2_fg)
img1[offset:rows+offset, offset:cols+offset] = dst
cv.imshow('res',img1)
cv.waitKey(0)
cv.destroyAllWindows()